package com.koron.configure.prior.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.util.CodeTools;
import com.koron.common.core.util.StringUtils;
import com.koron.configure.prior.bean.PriorBean;
import com.koron.configure.prior.bean.dto.PriorAcc;
import com.koron.configure.prior.bean.dto.PriorQuery;
import com.koron.configure.prior.mapper.PriorMapper;
import com.koron.configure.prior.service.PriorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Api(tags = "抽查配置")
@RestController
@RequestMapping("/prior")
public class PriorController {

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    private EamUser getCurrUser() {
        return ThreadLocalContext.get();
    }

    /**
     * 服务对象
     */
    @Autowired
    private PriorService configureservice;


    @ApiOperation(value = "抽查配置-列表")
    @GetMapping("/queryList")
    public Response<PageInfo> queryList(PriorQuery dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize(), dto.getOrderBy());
        List<PriorBean> list = ADOConnection.runTask(getCurrDs(), factory -> {
            return factory.getMapper(PriorMapper.class).queryAllByNameAndDeptIdAndDeptName(dto);
        }, List.class);
        return Response.ok(new PageInfo<>(list));
    }

    @ApiOperation(value = "抽查配置-列表 无分页")
    @PostMapping("/queryListNoPage")
    public Response<List<PriorBean>> queryListNoPage(@RequestBody PriorQuery dto) {
        if (getCurrDs().equals("_default")) {
            return Response.ok(new ArrayList<>());
        }
        List<PriorBean> list = ADOConnection.runTask(getCurrDs(), factory -> {
            return factory.getMapper(PriorMapper.class).queryAllByNameAndDeptIdAndDeptName(dto);
        }, List.class);
        return Response.ok(list);
    }

    @ApiOperation(value = "抽查配置-已有人员")
    @PostMapping("/existence")
    public Response<List<String>> existence() {
        List<String> accounts = ADOConnection.runTask(getCurrDs(), factory -> {
            return factory.getMapper(PriorMapper.class).queryAllByid(null);
        }, List.class);
        return Response.ok(accounts);
    }

    @ApiOperation(value = "抽查配置-新建")
    @PostMapping("/addPrio")
    public Response<String> addPrio(@RequestBody PriorBean bean) {
        Assert.isFalse(CollUtil.isEmpty(bean.getAccs()), "帐号不能为空");
        Assert.isFalse(StringUtils.isNull(bean.getTicketPrior()) && StringUtils.isNull(bean.getWorkPrior()), "工单抽查优先权和两票抽查优先权不能同时为空");
        List<String> accounts = ADOConnection.runTask(getCurrDs(), factory -> factory.getMapper(PriorMapper.class).queryAllByid(null), List.class);

        List<PriorBean> list = new ArrayList();
        for (PriorAcc acc : bean.getAccs()) {
            Assert.isFalse(accounts.contains(acc.getAccount()), StrUtil.format("{} 该人员帐号已存在", acc));
            PriorBean convert = new PriorBean();
            BeanUtils.copyProperties(bean, convert);
            convert.setId(CodeTools.getCode32());
            convert.setCreateTime(new Date());
            convert.setCreateBy(getCurrUser().getAccount());
            convert.setCreateByName(getCurrUser().getName());
            convert.setAccount(acc.getAccount());
            convert.setName(acc.getName());
            convert.setDeptId(acc.getDeptId());
            convert.setDeptName(acc.getDeptName());
            list.add(convert);
        }

        String id = ADOConnection.runTask(getCurrDs(), factory -> {
            factory.getMapper(PriorMapper.class).batchInsert(list);
            return bean.getId();
        }, String.class);
        return Response.ok("成功", id);
    }

    @ApiOperation(value = "抽查配置-更新")
    @GetMapping("/updatePrio")
    public Response<String> updatePrio(PriorBean bean) {
        Assert.isFalse(StringUtils.isEmpty(bean.getAccount()), "帐号不能为空");
        Assert.isFalse(StringUtils.isNull(bean.getTicketPrior()) && StringUtils.isNull(bean.getWorkPrior()), "工单抽查优先权和两票抽查优先权不能同时为空");

        List<String> accounts = ADOConnection.runTask(getCurrDs(), factory -> factory.getMapper(PriorMapper.class).queryAllByid(bean.getId()), List.class);

        Assert.isFalse(accounts.contains(bean.getAccount()), "该人员帐号已存在");

        ADOConnection.runTask(getCurrDs(), factory -> {
            factory.getMapper(PriorMapper.class).updateByPrimaryKey(bean);
            return bean.getId();
        }, String.class);
        return Response.ok("成功", bean.getId());
    }

    @ApiOperation(value = "抽查配置-删除")
    @DeleteMapping("/deleteById")
    public Response<String> deleteById(@RequestParam(value = "ids") List<String> ids) {
        ADOConnection.runTask(getCurrDs(), factory -> factory.getMapper(PriorMapper.class).deleteById(ids), Integer.class);
        return Response.ok("成功");
    }

}
